<?php

namespace app\controller\admin;

// use app\BaseController;
use app\controller\AdminController;
use think\Request;

use app\middleware\AdminAuth;
use app\model\StoreModel;
// use app\model\User;
use think\facade\Db;

class UniAccount extends AdminController
{

    protected $middleware  = [
        AdminAuth::class    => []
    ];
    /**
     * 当前登录用户下的店铺列表
     */
    public function index( Request $request )
    {

        // 获取当前用户
        
        $auth = $request->auth;

        // return json( $auth );
        // 获取 用户 的关联 店铺
        $uid = $auth['user_id'];
        $data = Db::table('ims_uni_account')
            ->alias('a')
            ->join('ims_uni_account_users au', 'a.uniacid = au.uniacid')
            ->join('ims_users u', 'au.uid = u.uid')
            ->where('u.uid', $uid)
            ->select();
            
        return json( $data );
    }


    public function current( Request $request ){
        
        $uid = $request->auth['user_id'];

        $uniacid = $request->param('uniacid');
        
        if( $uniacid ){

            set_uniacid( $uid, $uniacid );
        }
        return get_uniacid( $uid );

    }

    /**
     * 创建新的店铺
     */
    public function create(Request $request ){

        $auth = $request->auth;
        $uid = $auth['user_id'];
        $shop_name = $request->param('shop_name');
        $shop_name = trim( $shop_name );
        if( empty( $shop_name ) ){

            return [
                'code'=>1,
                'message'=>'店铺名称不能为空',
            ];
        }
        
        $sameNameShop = StoreModel::searchByShopName( $shop_name );

        if( !empty( $sameNameShop ) ){
            return [
                'code'=>1,
                'message'=>'店铺名称已存在',
            ];

        }

        $create_data = [
            'name'=> $shop_name,
            'uid'=> $uid,
        ];
        $uniacid=StoreModel::createShop( $create_data );

    }
}
